﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.Web.Controllers
{
    public class SearchController : BaseController
    {

        public string keyword = string.Empty; //关健字
        public int page=1; //当前页码
        public int totalcount=0; //OUT数据总数
        public IArticleService _article { get; set; }
        public IActionResult Index()
        {
            int.TryParse(Request.Query["page"], out page);
            page = page > 0 ? page : 1;
            ViewBag.page = page;
            ViewBag.keyword = keyword;
            ViewBag.totalcount = totalcount;
            keyword = Request.Query["keyword"];
            if (keyword == null)
                keyword = "";
            if(!Utils.IsSafeSqlString(keyword))
            {
                return Redirect("/Web/Error/Index?msg=" + Utils.UrlEncode("非法查询")); ;
            }
            List<Article> list = _article.ArticleSearch(10, page, "where (title like '%" + keyword + "%' or zhaiyao like '%" + keyword + "%')", "add_time desc,id desc", out totalcount);
            string pageUrl = Utils.CombUrlTxt("/Web/Search/Index", "page={0}&keyword={1}", "__id__",this.keyword);
            ViewBag.pagelist = Utils.OutPageList(50, page, totalcount, pageUrl, 8);
            return View(list);
        }
    }
}